package com.zyy.smartcity1129.hd;

import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.gson.JsonObject;
import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.data.hdlist;
import com.zyy.smartcity1129.data.hdpl;
import com.zyy.smartcity1129.util.Http;
import com.zyy.smartcity1129.util.RvAd;

import java.util.List;

import okhttp3.RequestBody;

public class Hd1Fragment extends BaseFragment {

    private hdlist.RowsBean hdlist = HDFragment.hdList;
    private ImageView ig;
    private TextView tv;
    private WebView web;
    private RecyclerView rv;
    private hdbm hdbm1;
    private EditText edit;
    private Button btn;
    private hdbm tjlist;

    @Override
    protected int layId() {
        return R.layout.f_hd1;
    }

    @Override
    protected String title() {
        return "";
    }

    @Override
    protected boolean back() {
        return true;
    }

    @Override
    protected Boolean bottom() {
        return false;
    }

    @Override
    protected void init(View iv) {
        right.setText("活动报名");
        title.setText(hdlist.getName());
        right.setOnClickListener(V -> {
            th(() -> {
                hdbm bmlist = Http.getInstance().get("/prod-api/api/activity/signup/check?activityId=" + hdlist.getId(), null, hdbm.class, 1);
                if (!bmlist.isSignup) {
                    JsonObject jsonObject = new JsonObject();
                    jsonObject.addProperty("activityId", hdlist.getId());
                    hdbm1 = Http.getInstance().get("/prod-api/api/activity/signup", RequestBody.create(Http.m, jsonObject.toString()), hdbm.class, 2);
                }
                ui(() -> {
                    if (hdbm1 == null) {
                        Toast.makeText(getContext(), "您已报名", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getContext(), hdbm1.getMsg(), Toast.LENGTH_SHORT).show();
                    }
                });
            });
        });
        initView(iv);
        Glide.with(ig.getContext()).load(Http.base + hdlist.getImgUrl()).into(ig);
        web.loadUrl(hdlist.getContent());
        web.loadDataWithBaseURL(Http.base, hdlist.getContent(), "text/html", "utf-8", null);
        web.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
        th(() -> {
            List<hdpl.RowsBean> list = Http.getInstance().get("/prod-api/api/activity/comment/list?activityId=" + hdlist.getId(), null, hdpl.class, 1).getRows();
            RvAd<hdpl.RowsBean> ad = new RvAd<hdpl.RowsBean>(list, R.layout.rv_base) {
                private void initView(View vi) {
                    ig = (ImageView) vi.findViewById(R.id.ig);
                    tv1 = (TextView) vi.findViewById(R.id.tv1);
                    tv2 = (TextView) vi.findViewById(R.id.tv2);
                    tv3 = (TextView) vi.findViewById(R.id.tv3);
                    tv4 = (TextView) vi.findViewById(R.id.tv4);
                    tv5 = (TextView) vi.findViewById(R.id.tv5);
                }

                private TextView tv5;
                private TextView tv4;
                private TextView tv3;
                private TextView tv2;
                private TextView tv1;
                private ImageView ig;

                @Override
                protected void initR(View vi, hdpl.RowsBean rowsBean, int p) {
                    initView(vi);
                    Glide.with(ig.getContext()).load(rowsBean.getAvatar()).into(ig);
                    tv1.setText("作者：" + rowsBean.getNickName());
                    tv2.setText("       " + rowsBean.getContent());
                    tv3.setVisibility(View.GONE);
                    tv4.setVisibility(View.GONE);
                    tv5.setVisibility(View.GONE);

                }
            };
            ui(() -> {
                rv.setAdapter(ad);
                rv.setLayoutManager(new LinearLayoutManager(getContext()));
            });
        });
        btn.setOnClickListener(v->{
            th(()->{
                String s=edit.getText().toString().trim();
                if(!s.equals("")){
                    JsonObject jsonObject=new JsonObject();
                    jsonObject.addProperty("activityId",hdlist.getId());
                    jsonObject.addProperty("content",s);
                    tjlist = Http.getInstance().get("/prod-api/api/activity/comment",RequestBody.create(Http.m,jsonObject.toString()),hdbm.class,2);
                }
                ui(()->{
                    if(tjlist!=null)
                        Toast.makeText(getContext(), "提交成功，等待审核", Toast.LENGTH_SHORT).show();
                    else
                        Toast.makeText(getContext(), "内容为空", Toast.LENGTH_SHORT).show();
                });
            });
        });
    }

    private void initView(View iv) {
        ig = (ImageView) iv.findViewById(R.id.ig);
        tv = (TextView) iv.findViewById(R.id.tv);
        web = (WebView) iv.findViewById(R.id.web);
        rv = (RecyclerView) iv.findViewById(R.id.rvMyOrder);
        edit = (EditText) iv.findViewById(R.id.edit);
        btn = (Button) iv.findViewById(R.id.btn);
    }

    static class hdbm {

        /**
         * msg : 未报名
         * isSignup : false
         * code : 200
         */

        private String msg;
        private boolean isSignup;
        private int code;

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

        public boolean isIsSignup() {
            return isSignup;
        }

        public void setIsSignup(boolean isSignup) {
            this.isSignup = isSignup;
        }

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }
    }

}
